// pages/fabu/fabu.js
const app = getApp();

Page({
  data: {
    can : [],
    canType : -1,
    canPicker:[],
    food: [],
    foodType: -1,
    foodPicker: [],
    listtitle:'',
    listtext:'',
    getaddress:'',
    toaddress:'',
    username:'',
    userid:'0',
    listcost:'',
    userphone:'',
    listArr: [],
    addressArr:[],
    userId: 1,
    CustomBar: app.globalData.CustomBar,
    checkbox: [{
      value: 0,
      data:{
        coupononenum:app.data.userInfo.coupononenum
      },
      name: '满20减5',
      checked: false,
      hot: false,
    }, {
      value: 1,
      data:{
        coupontwonum:app.data.userInfo.coupontwonum
      },
      name: '满50减10',
      checked: false,
      hot: false,
    }, ],

    pay:0.00

  },

  
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  }, 
  showModal123(e) {
    wx.navigateTo({
      url: 'pages/shopfood/shopfood',
    })
  }, 
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },

  choswAddress(e){
    console.log(e);
    this.setData({
      toaddress: e.currentTarget.dataset.addressid
    })
  },

  showModal1(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },  
  hideModal1(e) {
    this.setData({
      modalName: null
    })
  },
  ChooseCheckbox(e) {
    let items = this.data.checkbox;
    let values = e.currentTarget.dataset.value;
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      if (items[i].value == values) {
        items[i].checked = !items[i].checked;
      }
    }
    this.setData({
      checkbox: items
    })
    
  },
  getaddress(){
    var that=this;
      wx.request({
        url: 'https://www.woodymidnight.cn/BCUHelper/api/address/selectaddressbyid',
        method:'POST',
        data:{
          userId : app.data.userInfo.userid
        },
        header:{'content-type':'application/x-www-form-urlencoded'},
        success: function (res) {
          var resData = res.data;
          console.log("数据",resData);
          console.log("this",this);
          that.setData({
            addressArr: resData.data
          })
        }
      }) 
  },

  getCan(){
    wx.request({
      url: 'https://www.woodymidnight.cn/BCUHelper/api/can/selectall',
      method:'POST',
      data:{
        
      },
      header:{'content-type':'application/x-www-form-urlencoded'},
      success: res=> {
        console.log(res);
        let canPicker = this.data.canPicker
        for(var i = 0; i < res.data.data.length; i++){
          canPicker.push(res.data.data[i].canname)
        }
        this.setData({
          can: res.data.data,
          canPicker: canPicker
        })
      }
    }) 
  },

  canChange(res){
    console.log(res);
    wx.request({
      url: 'https://www.woodymidnight.cn/BCUHelper/api/food/selectbycanid',
      method:'POST',
      data:{
        canId: this.data.can[res.detail.value].canid
      },
      header:{'content-type':'application/x-www-form-urlencoded'},
      success: res=> {
        console.log(res);
        let foodPicker = []
        for(var i = 0; i < res.data.data.length; i++){
          foodPicker.push(res.data.data[i].foodname)
        }
        this.setData({
          foodPicker: foodPicker,
          foodType: -1,
          food: res.data.data
        })
      }
    })
    this.setData({
      canType: res.detail.value,
    })
  },

  foodBindtap(res){
    if(this.data.canType == -1){
      wx.showToast({
        title: '请选择食堂',
        icon: 'error'
      })
    }
  },

foodchange(e){
  wx.navigateTo({
    url: '../shopfood/shopfood',
  })
},

  foodChange(res){
    this.setData({
      foodType: res.detail.value,
      pay: this.data.food[res.detail.value].foodcost
    })
    
  },
  
  creattask(){

    if (app.data.userInfo.userid == null) {
      wx.showToast({
        title: '请先登录',
        icon: 'error'
      })
    }else if(this.data.listtitle == '' || this.data.listtext == '' || this.data.canType == -1 || this.data.foodType == -1 || this.data.userphone == '' || this.data.toaddress == ''){
      wx.showToast({
        title: '请完善信息',
        icon: 'error'
      })
    }else if(app.data.userInfo.balance < this.data.pay){
      wx.showToast({
        title: '余额不足',
        icon: 'error'
      })
    }else{
      
      wx.request({
        url: 'https://www.woodymidnight.cn/BCUHelper/api/list/insertcanlist',
        method:'POST',
        header:{'content-type':'application/x-www-form-urlencoded'},
        data:{
          listtype: 0,
          listtitle: this.data.listtitle,
          listtext: this.data.listtext,
          toaddress: this.data.toaddress,
          username: app.data.userInfo.username,
          userphone: this.data.userphone,
          userid: app.data.userInfo.userid,
          userimage : app.data.userInfo.userimage,
          listcost: this.data.pay,
          canlistcanid: this.data.can[this.data.canType].canid,
          canlistcan: this.data.can[this.data.canType].canname,
          canlistfood: this.data.food[this.data.foodType].foodname,
          canlistfoodid: this.data.food[this.data.foodType].foodid,
        },
        success:(res)=>{
          if(this.data.data == null){
            this.setData({
              listtype : '',
              listtitle : '',
              listtext : '',
              getaddress : '',
              toaddress : '',
              userphone : '',
              listcost : ''
            })
          }
          console.log(res.data);
        
        }
      })
      wx.switchTab({
        url: '/pages/index/index',
      })
      wx.showToast({
        title: '创建成功',
        icon:'success',
        duration:1500
      })

    }

      
  },
  showDialogBtn(){
    var that=this;
      wx.request({
        url: 'https://www.woodymidnight.cn/BCUHelper/api/list/insertlist',
        method:'POST',
        data:{
          userid : this.data.userid,
          userphone: app.data.userInfo.userphone,
        },
        header:{'content-type':'application/x-www-form-urlencoded'},
      success: function (res) {
        var resData = res.data;
        console.log("数据",resData);
        console.log("this",this);
        that.setData({
          listArr: resData.data
        })
      }
      }) 
  },
  showDialogBtn: function (e) {
    this.setData({       
      showModal: true,
        userid :app.data.userInfo.userid,
    
    })
     
    },
    preventTouchMove: function () {
       
    },
    hideModal: function () {
       
      this.setData({
       
      showModal: false
       
      });
       
      },
      onCancel: function () {
       
        this.hideModal();
         
        },
         
         
        onConfirm: function () {
        
        wx.showToast({
         
        title: '提交成功',
         
        icon: 'success',
         
        duration: 2000,
         
   
        })
        wx.navigateTo({
          url: '/pages/index/index',
        })
         
        this.hideModal();
         
        },
    
  inlistcost:function(e){

    console.log(e);
    if(e.detail.value < this.data.pay){
      wx.showToast({
        title: '请勿少于'+this.data.pay+'元',
        icon:'error'
      })
      this.setData({
        pay: this.data.pay
      })
    }

    // console.log(e);
    // this.setData({
    //   listcost: e.detail.value,
    //   pay: e.detail.value,
    // })
    // if(this.data.checkbox[0].checked == true&&this.data.listcost>=20&&this.data.listcost<50){
    //   this.setData({
    //     listcost:e.detail.value-5,
    //     coupononenum:e.detail.value-1
    //   })
    // }
    // if(this.data.checkbox[1].checked == true&&this.data.listcost>=50){
    //   this.setData({
    //     listcost:e.detail.value-5,
    //     coupontwonum:e.detail.value-1
    //   })
    // }
    // if(this.data.checkbox[0].checked==true){
    //   this.data.checkbox[1].checked==false
    // }else if(this.data.checkbox[1].checked==true){
    //   this.data.checkbox[0].checked==false
    // }
  },
  inlisttitle:function(e){
    console.log(e);
    this.setData({
      listtitle:e.detail.value,
    })
  },
  inlisttext:function(e){
    console.log(e);
    this.setData({
       listtext:e.detail.value,
    })
  },
  ingetaddress:function(e){
    console.log(e);
    this.setData({
      getaddress:e.detail.value,
    })
  },
  intoaddress:function(e){
    console.log(e);
    this.setData({
      toaddress:e.detail.value,
    })
  },
  inusername:function(e){
    console.log(e);
    this.setData({
      username:e.detail.value,
    })
  },
   inuserphone:function(e){
    console.log(e);
    this.setData({
      userphone:e.detail.value,
    })
  },
  PickerChange(e) {
    console.log(e.detail.value);
    let x = parseInt(e.detail.value)
    this.setData({
      listtype: x
    })
  },
 
      

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      console.log(app.data.userInfo);
      this.getaddress();
      this.getCan()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})